<template>
  <div>
    <el-button
      @click="show_dialog"
      icon="el-icon-circle-plus"
      type="primary"
      round
      >新增</el-button
    >
    <!-- 文章列表 -->
    <el-table :data="hos.list" style="width: 100%">
      <el-table-column align="center" prop="name" label="医院名称" width="180">
      </el-table-column>
      <el-table-column
        align="center"
        prop="introduce"
        label="医院简介"
        width="180"
      >
      </el-table-column>
      <el-table-column align="center" prop="address" label="医院地址">
      </el-table-column>

      <el-table-column align="center" prop="flags" label="标记">
      </el-table-column>

      <el-table-column align="center" prop="longitude" label="经度">
      </el-table-column>

      <el-table-column align="center" prop="latitude" label="纬度">
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template v-slot="scope">
          <el-button
            @click="delete_hos(scope.row.id)"
            icon="el-icon-delete-solid"
            type="danger"
            round
            style="margin-right: 5px"
            size="small"
            >删除</el-button
          >
          <el-button
            @click="edit_hos(scope.row)"
            icon="el-icon-position"
            type="info"
            round
            size="small"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="obj.pageSize"
        :current-page="obj.page"
        @current-change="change"
        :total="hos.total"
      >
      </el-pagination>
    </div>
    <!-- 模态框 -->
    <el-dialog :title="title" :visible.sync="dialog_hos" width="60%">
      <el-form :model="form_hos" :rules="rules">
        <el-form-item
          label="医院名称"
          :label-width="formLabelWidth_hos"
          prop="name"
        >
          <el-input v-model="form_hos.name"></el-input>
        </el-form-item>
        <el-form-item
          label="医院简介"
          :label-width="formLabelWidth_hos"
          prop="introduce"
        >
          <el-input v-model="form_hos.introduce"></el-input>
        </el-form-item>
        <el-form-item
          label="省市区"
          :label-width="formLabelWidth_hos"
          prop="pca"
        >
          <el-cascader
            v-model="form_hos.pca"
            :options="pca"
            :props="{ expandTrigger: 'hover', label: 'name', value: 'name' }"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          :label-width="formLabelWidth_hos"
          prop="address"
        >
          <el-input v-model="form_hos.address"></el-input>
        </el-form-item>
        <el-form-item label="标记" :label-width="formLabelWidth_hos">
          <el-input v-model="form_hos.flags"></el-input>
        </el-form-item>
        <el-form-item
          label="经度"
          :label-width="formLabelWidth_hos"
          prop="longitude"
        >
          <el-input v-model="form_hos.longitude"></el-input>
        </el-form-item>
        <el-form-item
          label="纬度"
          :label-width="formLabelWidth_hos"
          prop="longitude"
        >
          <el-input v-model="form_hos.latitude"></el-input>
        </el-form-item>
        <el-form-item label="打开地图选择经纬度" label-width=140>
          <el-button @click="show_map">打开地图</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialog_hos = false">取 消</el-button>
        <el-button type="primary" @click="save_hos">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择地点" :visible.sync="dialog_map" width="60%">
      <div id="Map" style="height: 400px"></div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialog_map = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import pca from "../../assets/pca.json";
import { get, post } from "@/utils/request";
export default {
  computed: {
    ...mapState("hos", ["hos"]),
  },
  watch: {
    obj: {
      handler: function () {
        this.findHos(this.obj);
      },
      deep: true,
    },
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: "请输入医院名称", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入医院地址", trigger: "blur" },
        ],
        pca: [
          { required: true, message: "请输入医院名称", trigger: "blur" },
        ],
        longitude: [
          { required: true, message: "请输入经度", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "请输入纬度", trigger: "blur" },
        ],
      },
      map: {},
      dialog_map: false,
      pca,
      obj: {
        page: 1,
        pageSize: 10,
      },
      title: "新增医院",
      dialog_hos: false,
      form_hos: {},
      formLabelWidth_hos: "120px",
    };
  },
  created() {
    this.findHos(this.obj);
  },
  methods: {
    handleChange(){},
    ...mapActions("hos", ["findHos", "saveHos", "delHos"]),
    initMap() {
      this.map = new AMap.Map("Map", {
        resizeEnable: true,
        zoom: 15,
      });
      this.map.setCity(this.form_hos.pca[2]);
    },
    // 给地图绑定单击事件
    clickOn() {
      this.map.on("click", this.showInfoClick);
    },
    // 获取经纬度
    showInfoClick(e) {
      this.form_hos.longitude = e.lnglat.getLng();
      this.form_hos.latitude = e.lnglat.getLat();
    },
    show_map() {
      this.dialog_map = true;
      setTimeout(() => {
        this.initMap();
        this.clickOn();
      }, 1000);
    },
    show_dialog() {
      this.form_hos = {};
      this.dialog_hos = true;
    },
    async delete_hos(id) {
      let res = await get("/hospital/deleteById", { id });
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.findHos(this.obj);
      }
    },
    edit_hos(val) {
      console.log(val);
      this.title = "修改医院信息";
      this.dialog_hos = true;
      //深拷贝
      this.form_hos = JSON.parse(JSON.stringify(val));
      this.form_hos.pca = [
        this.form_hos.province,
        this.form_hos.city,
        this.form_hos.area,
      ];
      //修改的
      this.findHos(this.obj);
    },
    change(val) {
      this.obj.page = val;
    },
    async save_hos() {
      this.form_hos.province = this.form_hos.pca[0];
      this.form_hos.city = this.form_hos.pca[1];
      this.form_hos.area = this.form_hos.pca[2];
      let res = await post("/hospital/saveOrUpdate", this.form_hos);
      console.log(res);
      if (res.status == 200) {
        this.$message({
          message: res.message,
          type: "success",
        });
        this.dialog_hos = false;
      }
      this.findHos(this.obj);
    },
  },
};
</script>
<style  scoped>
.pagination {
  position: fixed;
  right: 20px;
  bottom: 20px;
}
</style>